//
//  Tabbar.swift
//  weibo1
//
//  Created by 代亚洲 on 16/4/16.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class Tabbar: UITabBar {
    
//    监听方法调用的闭包
    var composeButtonClickClosure: (()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
//        addSubview(addbutton)
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setupUI()
    }
    
    private func setupUI() {
        addSubview(addbutton)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //        设置加号button的中心点
        addbutton.center = CGPointMake(self.frame.size.width/2, self.frame.size.height/2)
        //        设置每一个按钮的宽度
        let itemw = self.frame.size.width/5
        //      记录遍历的是哪一个item
        var index = 0
        for value in subviews {
            
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                value.frame.size.width = itemw
                //                当是第三个宽度的时候 跳过去 加一个宽度
                if index == 2{
                    index++
                }
                //                设置x值
                value.frame.origin.x = itemw * CGFloat(index)
                
                index++
                
            }
        }
    }
    
    private lazy var addbutton : UIButton = {
        
        let button = UIButton()
        
        //    监听按钮的点击事件
        button.addTarget(self, action: Selector("composeButtonClick"), forControlEvents: UIControlEvents.TouchUpInside)
        //    高亮状态图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        //    普通状态下图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        //    背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        //     背景图片高亮状态
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        // 内容大多，控件就设置成多大
        button.sizeToFit()
        
        return button
    }()
    
    
    // MARK: - 监听方法
    
    func composeButtonClick(){
        composeButtonClickClosure?()
        print("发送微博界面")
    }
    
}
